<?php

require("config.inc.php");
require("database_manager.php");
require("leveltype.php");
require("node.php");
require("leaf.php");
require("edge.php");
require("company.php");

set_time_limit(60);


/////////////////////////////////////////////////////////////////////////////
// Connect to database:
/////////////////////////////////////////////////////////////////////////////
debug("Connecting to database...");
DatabaseManager::getInstance(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE)->connect();



/////////////////////////////////////////////////////////////////////////////
// Leveltype class testing.
/////////////////////////////////////////////////////////////////////////////

// LOAD LEVELTYPE
$leveltype = new Leveltype();
$randomLeveltypeID = 3;
$leveltype->load($randomLeveltypeID);
$leveltype->printLeveltypeData("Random leveltype");

// INSERT LEVELTYPE
$newLeveltype = new Leveltype(5, "Next Level", "New level type");

$newLeveltype->insert();
$newLeveltype->printLeveltypeData("NEW LEVELTYPE inserted");

debug("Attempting to insert same leveltype a second time...");
$newLeveltype->insert();
$newLeveltype->printLeveltypeData("NEW LEVELTYPE inserted again (should have failed)");

// UPDATE LEVELTYPE
$newLeveltype->setDescription("new description");
$newLeveltype->update();
$newLeveltype->load($newLeveltype->getID());
$newLeveltype->printLeveltypeData("NEW LEVELTYPE updated");

// DELETE LEVELTYPE
$newLeveltype->delete();
$newLeveltype->load($newLeveltype->getID());
$newLeveltype->printLeveltypeData("NEW LEVELTYPE deleted");

// LOAD ALL LEVELTYPE RECORDS
$allLeveltypes = Leveltype::loadAllLeveltypes();
Leveltype::printLeveltypesData($allLeveltypes, "All leveltypes");






/////////////////////////////////////////////////////////////////////////////
// Load the root node from the database:
/////////////////////////////////////////////////////////////////////////////
$rootNode = new Node();
$rootNode->load(ROOT_NODE_ID);
$rootNode->printNodeData("ROOT NODE");

/////////////////////////////////////////////////////////////////////////////
// Modify the root node and save it to the database before reloading it again:
/////////////////////////////////////////////////////////////////////////////
$rootNode->setWebaddress("http://rootocri2.com/");
$rootNode->update();
$rootNode->load(ROOT_NODE_ID);
$rootNode->printNodeData("ROOT NODE after update and reload");


/////////////////////////////////////////////////////////////////////////////
// Load and display some other nodes:
/////////////////////////////////////////////////////////////////////////////
$cleantechNode = new Node();
$cleantechNode->load(1005); // 1005 is "Cleantech" node (level 2)
$cleantechNode->printNodeData("Cleantech NODE");

$otherNode = new Node();
$otherNode->load(1011);  // 1011 is "Other" node (level 2)
$otherNode->printNodeData("Other NODE");


/////////////////////////////////////////////////////////////////////////////
// Insert a new node (child to root node):
/////////////////////////////////////////////////////////////////////////////
$newNode = new Node();
$newNode->setParentnodeID($rootNode->getID());
$newNode->setLeveltypeID(2);
$newNode->setName("Sheep Technology");
$newNode->setDescription("Sheep Technology Galore");
$newNode->setWebaddress("http://sheepish.com/");
$newNode->insert();
$newNode->printNodeData("NEW NODE inserted");

/////////////////////////////////////////////////////////////////////////////
// Delete the new node:
/////////////////////////////////////////////////////////////////////////////
debug("Deleting node just inserted...");
$newNode->delete();  // note that object $newNode is still in memory and populated
$deletedNode = new Node();
$deletedNode->load($newNode->getID());
$deletedNode->printNodeData("DELETED NODE (should show empty values)");


/////////////////////////////////////////////////////////////////////////////
// Load an array of nodes.  Load the child nodes of the root node:
/////////////////////////////////////////////////////////////////////////////
$rootChildrenNodes = Node::loadChildNodesOfNodeByID(ROOT_NODE_ID);
Node::printNodesData($rootChildrenNodes, "Child nodes of root node by ID");
// Same thing but use the root Node object instead of the ID
$rootChildrenNodes = Node::loadChildNodesOfNode($rootNode);
Node::printNodesData($rootChildrenNodes, "Child nodes of root node by Node");
// In both cases, $rootChildrenNodes is an array of Node objects.

/////////////////////////////////////////////////////////////////////////////
// Load an array of nodes.  Load all nodes that are level 2:
/////////////////////////////////////////////////////////////////////////////
$level2Nodes = Node::loadNodesOfLevelID(2);
Node::printNodesData($level2Nodes, "Level 2 nodes");




/////////////////////////////////////////////////////////////////////////////
// Load the Leaf records for a particular node into an array.
// This includes ALL leaf records, direct and indirect.
/////////////////////////////////////////////////////////////////////////////
$leafs = Leaf::loadLeafsOfNodeID(1005); // 1005 is "Cleantech" node (level 2)
Leaf::printLeafsData($leafs, "Leafs of node ID 1005 (Cleantech (level2))");


/////////////////////////////////////////////////////////////////////////////
// Load the Leaf records for a particular node into an array.
// This includes leaf records that are directly under a node.
/////////////////////////////////////////////////////////////////////////////
$leafs = Leaf::loadLeafsOfParentNodeID(1026); // 1026 is "Software" node (level 3)
Leaf::printLeafsData($leafs, "Direct Leafs of node ID 1026 (Software (level 3))");




/////////////////////////////////////////////////////////////////////////////
// Load a leaf from the database:
/////////////////////////////////////////////////////////////////////////////
$leaf = new Leaf();
$leaf->load(107293);
$leaf->printLeafData("Leaf record with ID 107293");

/////////////////////////////////////////////////////////////////////////////
// Insert a new leaf into the database:
// Linked it directly to node 1026 (Software - level 3):
/////////////////////////////////////////////////////////////////////////////
$newLeaf = new Leaf();
$newLeaf->setParentnodeID(1026); // 1026 is "Software" node (level 3)
$newLeaf->setObjectID(11142); // some arbitrary object id
$newLeaf->setTablename(TABLE_OBJECT); // TABLE_OBJECT is defined in config.inc.php
if (!$newLeaf->existsInDatabase()) {
    if (!$newLeaf->insert()) {
        // Will not insert if record already exists
        //   (parentnode_id and object_id combination already exists in a leaf record)
        // ID of new Leaf populated in object.
        debug("Leaf not inserted");
    } else {
        $newLeaf->printLeafData("Newly inserted Leaf object");
        $newLeaf->delete(); // just to keep things a bit clean
        debug("Deleted new leaf record.");
    }
} else {
    debug("Leaf already exists!");
}





/////////////////////////////////////////////////////////////////////////////
// Load edge record from database.
/////////////////////////////////////////////////////////////////////////////
$edgeBetweenTwoNodes = Edge::loadEdgeBetweenTwoNodesByNodeID(1005, 1011);
$edgeBetweenTwoNodes->printEdgeData("Edge between nodes 1005 and 1011");

// insert(), update(), delete() methods are also provided.  However, edge
// records should only be manipulated by the updateEdges.php script, which
// should be invoked to refresh all edge records.








/////////////////////////////////////////////////////////////////////////////
// Company class testing.
/////////////////////////////////////////////////////////////////////////////


// LOAD COMPANY
$company = new Company();
$randomCompanyID = 11939;
$company->load($randomCompanyID);
$company->printCompanyData("Random company");


// INSERT COMPANY
$newCompany = new Company(null, "Degsites Web Development", "Boonies", "",
                          "Rocky Mountain House", "T4T 2A1", 1, 2010,
                          "http://degsites.com", "Jack of all trades IT guy");

$newCompany->insert();
$newCompany->printCompanyData("NEW COMPANY inserted");


print("Attempting to insert same company a second time...\n");
$newCompany->insert();
$newCompany->printCompanyData("NEW COMPANY inserted again (should have failed)");

// UPDATE COMPANY
$newCompany->setWebaddress("thegraves.ca");
$newCompany->update();
$newCompany->load($newCompany->getID());
$newCompany->printCompanyData("NEW COMPANY updated");

// DELETE COMPANY
$newCompany->delete();
$newCompany->load($newCompany->getID());
$newCompany->printCompanyData("NEW COMPANY deleted");



$allCompanies = Company::loadAllCompanies();
Company::printCompaniesData($allCompanies, "All companies");












/////////////////////////////////////////////////////////////////////////////
// Disconnect from database:
/////////////////////////////////////////////////////////////////////////////
DatabaseManager::getInstance()->disconnect();
debug("Disconnected from database.");






function debug($message) {
    print($message . "\n");
}
?>

